// pages/ComfirmOrder/ComfirmOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ticketDetail: [{
      ticImg: '001.jpg'
    }],
    showup: false,
    showDetail: true,
    priceList: [],
    isActive: 5,
    showNumber: false,
    ticNumber: 1,
    isreduceNumber: false,
    isaddNumber: true,
    totalPrice: 0,
    onePrice: 0,
    showTicketFiles: true,
    showSelectPrice: true,
    viewerlists: [],
    checkViewers: [],
    checkedview: [],
    agreed: false
  },

  //打开详细
  detail() {
    let self = this
    self.setData({
      showup: true,
      showDetail: false
    })
  },

  //关闭详细
  detail1() {
    let self = this
    self.setData({
      showup: false,
      showDetail: true
    })
  },

  //关闭详细
  closeDetail() {
    let self = this
    self.setData({
      showDetail: true,
      showup: false
    })
  },

  //打开票档
  ticketPrice() {
    let self = this
    self.setData({
      showTicketFiles: false
    })
  },

  //票档点击事件
  priceList(e) {
    let self = this
    let index = e.currentTarget.dataset.index
    self.setData({
      isActive: index,
      showNumber: true,
      totalPrice: self.data.priceList[index].price * self.data.ticNumber,
      onePrice: self.data.priceList[index].price,
      showSelectPrice: false
    })
  },

  // 数量减一
  reduceNumber() {
    let self = this
    if (self.data.ticNumber > 1) {
      self.setData({
        isreduceNumber: true,
        ticNumber: self.data.ticNumber - 1
      })
      if (self.data.ticNumber = 1) {
        self.setData({
          isreduceNumber: false,
          isaddNumber: true,
          totalPrice: self.data.onePrice * self.data.ticNumber
        })
      }
    }
  },

  // 数量加一
  addNumber() {
    let self = this
    if (self.data.ticNumber < 2) {
      self.setData({
        ticNumber: self.data.ticNumber + 1
      })
      if (self.data.ticNumber = 2) {
        self.setData({
          isreduceNumber: true,
          isaddNumber: false,
          totalPrice: self.data.onePrice * self.data.ticNumber
        })
      }
    } else if (self.data.ticNumber = 2) {
      wx.showToast({
        title: '已达到限购上限',
        icon: 'none'
      })
    }
  },

  //关闭票档
  closeTicketFiles() {
    let self = this
    self.setData({
      showTicketFiles: true
    })
  },

  //同意条款
  agreed() {
    let self = this
    if (self.data.agreed == false) {
      self.setData({
        agreed: true
      })
    } else if (self.data.agreed == true) {
      self.setData({
        agreed: false
      })
    }
  },

  //新增观影人
  addViewers() {
    wx.navigateTo({
      url: '/pages/newwatcher/newwatcher',
    })
  },

  //选择观影人
  selectViewer(e) {
    let self = this
    let index = e.currentTarget.dataset.index
    self.setData({
      ['checkViewers[' + index + '].checkViewer']: !self.data.checkViewers[index].checkViewer
    })
    if (self.data.checkViewers[index].checkViewer == true) {
      self.data.checkedview.push(self.data.viewerlists[index].viewer);
    } else {
      self.data.checkedview.forEach((item, index1) => {
        if (item == self.data.viewerlists[index].viewer) {
          self.data.checkedview.splice(index1, 1)
        }
      })
    }
  },

  //提交订单
  submitOrder() {
    let self = this
    if (self.data.checkedview.length == 0) {
      wx.showToast({
        title: '请选择观影人',
        icon: 'none'
      })
    } else if (self.data.showSelectPrice == true) {
      wx.showToast({
        title: '请选择票价',
        icon: 'none'
      })
    } else if (self.data.agreed == false) {
      wx.showToast({
        title: '请阅读服务条款并同意',
        icon: 'none'
      })
    } else {
      let ticId = self.data.ticketDetail[0].ticId
      let year = new Date().getFullYear().toString();
      let month = (new Date().getMonth() + 1).toString();
      let month1 = new Date().getDate().toString();
      let hours = new Date().getHours().toString();
      let minutes = new Date().getMinutes().toString();
      if (hours < 10) {
        hours = "0" + hours;
      }
      if (minutes < 10) {
        minutes = "0" + minutes;
      }
      if (month < 10) {
        month = "0" + month;
      }
      if (month1 < 10) {
        month1 = "0" + month1;
      }
      let orderId = year + month + month1 + hours + minutes;
      let orderDate = year + "-" + month + "-" + month1 + " " + hours + ":" + minutes;
      wx.getStorage({
        key: 'userName',
        success: function (res) {
          wx.request({
            url: "http://localhost:3000/placeOrder",
            method: 'POST',
            data: {
              ticId: ticId,
              status: 2,
              userName: res.data,
              ticNumber: self.data.ticNumber,
              price: self.data.totalPrice,
              viewer: self.data.checkedview,
              orderDate: orderDate,
              orderDate1: new Date().getTime(),
              orderId: orderId
            },
            success(res) {
              let result = res.data;
              if (result == "提交成功") {
                wx.showToast({
                  title: '提交成功'
                })
                wx.setStorage({
                  key: 'orderId',
                  data: orderId,
                })
                wx.navigateTo({
                  url: '/pages/payment/payment'
                })
              } else if (result == "提交失败") {
                wx.showToast({
                  title: '提交失败',
                  icon: 'none'
                })
              }
            }
          })},
      })
    }
  },

  //获取演出详情
  getTicketDetail() {
    let self = this
    wx.getStorage({
      key: 'ticId',
      success: function(res) {
        wx.request({
          url: "http://localhost:3000/getTicketDetail",
          method: 'POST',
          data: {
            ticId: res.data
          },
          success(res) {
            let result = res.data;
            if (result) {
              self.setData({
                ticketDetail: result
              })
            }
          }
        })
      },
    })
  },

  //获取票档
  getGrade() {
    let self = this
    wx.getStorage({
      key: 'ticId',
      success: function(res) {
        wx.request({
          url: "http://localhost:3000/getGrade",
          method: 'POST',
          data: {
            ticId: res.data
          },
          success(res) {
            let result = res.data;
            if (result) {
              self.setData({
                priceList: result
              })
            }
          }
        })
      },
    })
  },

  //获取观影人
  getViewers() {
    let self = this
    wx.getStorage({
      key: 'userName',
      success: function(res) {
        wx.request({
          url: "http://localhost:3000/showViewer",
          method: 'POST',
          data: {
            userName: res.data
          },
          success(res) {
            let result = res.data;
            if (result) {
              self.setData({
                viewerlists: result
              })
              for (let i = 0; i < self.data.viewerlists.length; i++) {
                let checkViewer = {
                  checkViewer: false
                };
                self.data.checkViewers.push(checkViewer);
                for (let i = 0; i < self.data.viewerlists.length; i++) {
                  let front = self.data.viewerlists[i].userIdentityCard.substring(0, 3);
                  let behind = self.data.viewerlists[i].userIdentityCard.substring(15, 18);
                  self.setData({
                    ['viewerlists[' + i + '].userIdentityCard']: front + "************" + behind
                  })
                }
              }
            }
          }
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this
    self.getTicketDetail()
    self.getViewers()
    self.getGrade()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})